<template>
	<div class="box">
		<Contract
			v-if="type !== 'preview' || env === 'flowable'"
			v-model="formData"
			ref="contract"
			:state="state"
			:type="type"></Contract>
		<Contract1
			v-if="
				type == 'preview' &&
				env !== 'flowable' &&
				formData.leaseContract &&
				formData.leaseContract.contractTemplateSchemeType != '2'
			"
			v-model="formData"
			ref="contract1"
			state="readonly"
			:type="type"></Contract1>
	</div>
</template>

<script>
	import FormItems from '@/views/finance/manage/formItems/index.vue';
	import FormItem from '@/views/finance/reimburs/formItem/index.vue';
	import Contract from '../carInfoChunk/contract/index.vue';
	import Contract1 from '../carInfoChunk/contract/index1.vue';
	export default {
		components: { FormItems, FormItem, Contract, Contract1 },

		props: {
			value: {
				required: false,
			},
			state: {
				required: false,
			},
			type: {
				required: false,
			},
			env: {
				required: false,
			},
		},

		data() {
			return {
				ContractData: {},
			};
		},
		computed: {
			formData: {
				get() {
					return this.useVModel.call(this);
				},
				set(val) {
					this.$emit('input', val);
				},
			},
		},
		created() {
			this.formData.leaseContract ??= {
				contractTemplateId: '',
				contractTemplateCode: '',
				contractTemplateName: '',
				contractTemplateSchemeType: '',
				contractTemplateType: '',
				snapshootData: '',
			};
			this.formData.leaseContract.contractTemplateSchemeType ??= '2';
		},
		mounted() {},
		methods: {
			valid() {
				let result1 = this.getFormData();
				return new Promise(async resolve => {
					let result = true;
					let refs = Object.values(this.$refs);
					for (let index = 0; index < refs?.length; index++) {
						const ref = refs[index];
						if (
							Object.prototype.toString.call(ref) ==
							'[object Array]'
						) {
							for (let i = 0; i < ref.length; i++) {
								const element = ref[i];
								if (element?.valid) {
									let rc = await element?.valid?.();
									result = result && rc;
								}
								if (element?.validate) {
									let rf = await this._valid(ref);
									result = result && rf;
								}
								if (element?.form_validate) {
									let rf = await this._valid(
										ref,
										'form_validate',
									);
									result = result && rf;
								}
							}
						} else {
							if (ref?.valid) {
								let rc = await ref?.valid?.();
								result = result && rc;
							}
							if (ref?.validate) {
								let rf = await this._valid(ref);
								result = result && rf;
							}
							if (ref?.form_validate) {
								let rf = await this._valid(
									ref,
									'form_validate',
								);
								result = result && rf;
							}
						}
					}
					result = result && result1;
					resolve(result);
				});
			},
			getForm(type) {
				let data = this._.cloneDeep(this.$refs.contract.getForm(type));
				if (!this.notEmpty(data)) {
					return false;
				}
				this.formData.leaseContract.contractTemplateName = '';
				this.formData.leaseContract.contractTemplateId = '';
				this.formData.leaseContract.contractTemplateCode = '';
				this.formData.leaseContract.snapshootData = '';
				this.formData.leaseContract.contractTemplateType = '1'; // 合同模板类型 固定
				if (
					this.formData.leaseContract.contractTemplateSchemeType ==
					'2'
				) {
					this.formData.leaseContract.contractTemplateName =
						data?.newTemplateObj?.contractTemplateName;
					this.formData.leaseContract.contractTemplateId =
						data?.newTemplateObj?.contractTemplateId;
					this.formData.leaseContract.contractTemplateCode =
						data?.newTemplateObj?.contractTemplateCode;
				} else {
					let form = {};
					form.applicationType =
						this.formData.leaseContract.contractTemplateSchemeType;
					form.newTemplateId = data?.newTemplateObj?.value;
					form.newTemplateName = data?.newTemplateObj?.label;
					form.templateClassification =
						data?.newTemplateObj?.templateClassification;
					form.onlyThis = data?.onlyThis;
					form.specialAgreement = data?.specialAgreement;
					form.remark = data?.remark;
					form.annex = data?.annex;
					form.contractTemplate = data;

					this.formData.leaseContract.snapshootData =
						this.Base64.encode(JSON.stringify(form));
				}
				return true;
			},
			getFormData(type) {
				let data = this._.cloneDeep(
					this.$refs.contract.getFormData(type),
				);
				if (!this.notEmpty(data)) {
					return false;
				}
				this.formData.leaseContract.contractTemplateName = '';
				this.formData.leaseContract.contractTemplateId = '';
				this.formData.leaseContract.contractTemplateCode = '';
				this.formData.leaseContract.snapshootData = '';
				this.formData.leaseContract.contractTemplateType = '1'; // 合同模板类型 固定
				if (
					this.formData.leaseContract.contractTemplateSchemeType ==
					'2'
				) {
					this.formData.leaseContract.contractTemplateName =
						data?.newTemplateObj?.contractTemplateName;
					this.formData.leaseContract.contractTemplateId =
						data?.newTemplateObj?.contractTemplateId;
					this.formData.leaseContract.contractTemplateCode =
						data?.newTemplateObj?.contractTemplateCode;
				} else {
					let form = {};
					form.applicationType =
						this.formData.leaseContract.contractTemplateSchemeType;
					form.newTemplateId = data?.newTemplateObj?.value;
					form.newTemplateName = data?.newTemplateObj?.label;
					form.templateClassification =
						data?.newTemplateObj?.templateClassification;
					form.onlyThis = data?.onlyThis;
					form.specialAgreement = data?.specialAgreement;
					form.remark = data?.remark;
					form.annex = data?.annex;
					form.contractTemplate = data;

					this.formData.leaseContract.snapshootData =
						this.Base64.encode(JSON.stringify(form));
				}
				return true;
			},
		},
	};
</script>

<style lang="scss" scoped></style>
